package org.example.y24.m10;

import java.util.Scanner;

public class ShortestRemainingTime {
    //构建进程类：进程名，创建时间，运行时间，状态，剩余时间
    static class Process{
        String name;//进程名
        int createTime;//创建时间
        int runTime;//运行时间
        String status;//状态
        int remainTime;//剩余时间
        public Process(String name, int createTime, int runTime){
            this.name = name;
            this.createTime = createTime;
            this.runTime = runTime;
            this.remainTime = runTime-createTime;
        }
    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("请输入进程数量：");
        Integer n;//进程数量
        n = scanner.nextInt();
        Process[] processes = new Process[n];
        for (int i = 0; i < n; i++) {
            System.out.print("请输入进程" + (i + 1) + "的创建时间&运行时间-->");
            int createTime = scanner.nextInt();
            int runTime = scanner.nextInt();
            processes[i] = new Process("P" + (i + 1), createTime, runTime);
        }
        //对进程按创建时间进行排序
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n - i - 1; j++) {
                if (processes[j].createTime > processes[j + 1].createTime) {
                    Process temp = processes[j];
                    processes[j] = processes[j + 1];
                    processes[j + 1] = temp;
                }
            }
        }
        Integer result = Integer.MAX_VALUE;//剩余时间最少的进程的运行时间
        Integer flag = 0;//当前进程编号
        Boolean c = true;
        int createTime = 0;
        int endTime = 0;
        while (processesIsZero(processes)){
            for (int i = 0; i < n; i++) {
                if (processes[i].remainTime == 0) {
                    //剩余时间为0，说明进程已经完成，跳过
                    continue;
                }
                if (processes[i].createTime <= i){
                    //进程已创建，开始执行
                    if (result > processes[i].remainTime){
                        result = processes[i].remainTime;
                        if (flag != i) {

                        }
                    }
                }

            }
        }


    }

    private static boolean processesIsZero(Process[] processes) {
        for (int i = 0; i < processes.length; i++){
            if (processes[i].remainTime != 0){
                //剩余时间不为0，说明还有进程没有完成，返回true
                return true;
            }
        }
        //所有进程都完成，返回false
        return false;
    }
}
